package com.agileai.crm.controller.visit;

import java.util.Date;
import java.util.List;

import com.agileai.crm.bizmoduler.visit.VisitManage;
import com.agileai.crm.common.PrivilegeHelper;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardListHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;

public class VisitManageListHandler extends StandardListHandler {
	public VisitManageListHandler() {
		super();
		this.editHandlerClazz = VisitManageEditHandler.class;
		this.serviceId = buildServiceId(VisitManage.class);
	}
	
	public ViewRenderer prepareDisplay(DataParam param){
		User user = (User) this.getUser();
		PrivilegeHelper privilegeHelper = new PrivilegeHelper(user);
		if (!privilegeHelper.isSalesDirector()) {
			param.put("currentUserCode", user.getUserId());
			setAttribute("hasRight", false);
		} else {
			param.put("currentUserCode", "");
			setAttribute("hasRight", true);
		}
		mergeParam(param);
		initParameters(param);
		this.setAttributes(param);
		List<DataRow> rsList = getService().findRecords(param);
		this.setRsList(rsList);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}

	protected void processPageAttributes(DataParam param) {
		initMappingItem("VISIT_TYPE", FormSelectFactory.create("VISIT_TYPE")
				.getContent());
		initMappingItem("VISIT_STATE", FormSelectFactory.create("CUST_STATE")
				.getContent());
	}

	protected void initParameters(DataParam param) {
		initParamItem(
				param,
				"sdate",
				DateUtil.getDateByType(DateUtil.YYMMDD_HORIZONTAL,
						DateUtil.getBeginOfMonth(new Date())));
		initParamItem(
				param,
				"edate",
				DateUtil.getDateByType(DateUtil.YYMMDD_HORIZONTAL,
						DateUtil.getDateAdd(new Date(), DateUtil.DAY, 1)));
		initParamItem(param, "visitCustName", "");
		initParamItem(param, "visitUserName", "");
	}

	@PageAction
	public ViewRenderer doConfirmRequestAction(DataParam param) {
		storeParam(param);
		return new DispatchRenderer(getHandlerURL(this.editHandlerClazz) + "&"
				+ OperaType.KEY + "=confirm&comeFrome=confirm");
	}

	@PageAction
	public ViewRenderer doCounterConfirmRequestAction(DataParam param) {
		storeParam(param);
		return new DispatchRenderer(getHandlerURL(this.editHandlerClazz) + "&"
				+ OperaType.KEY + "=counterConfirm&comeFrome=counterConfirm");
	}
	

	protected VisitManage getService() {
		return (VisitManage) this.lookupService(this.getServiceId());
	}
}
